package com.stx.jeepay.payment.controller.transfer;

import com.stx.jeepay.core.entity.TransferOrderEntity;
import com.stx.jeepay.core.expection.BizException;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.core.service.TransferOrderService;
import com.stx.jeepay.payment.controller.ApiController;
import com.stx.jeepay.payment.rqrs.transfer.QueryTransferOrderRQ;
import com.stx.jeepay.payment.rqrs.transfer.QueryTransferOrderRS;
import com.stx.jeepay.payment.service.ConfigContextQueryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户转账单查询controller
 */
@Slf4j
@RestController
public class QueryTransferOrderController extends ApiController {
    @Autowired
    private TransferOrderService transferOrderService;
    @Autowired
    private ConfigContextQueryService configContextQueryService;

    /**
     * 查单接口
     **/
    @RequestMapping("/api/transfer/query")
    public ApiRes queryTransferOrder() {
        QueryTransferOrderRQ rq = getRQByWithMchSign(QueryTransferOrderRQ.class);

        if (StringUtils.isAllEmpty(rq.getMchOrderNo(), rq.getTransferId())) {
            throw new BizException("mchOrderNo 和 transferId不能同时为空");
        }

        TransferOrderEntity refundOrder =
                transferOrderService.queryMchOrder(rq.getMchNo(), rq.getMchOrderNo(), rq.getTransferId());
        if (refundOrder == null) {
            throw new BizException("订单不存在");
        }
        QueryTransferOrderRS bizRs = QueryTransferOrderRS.buildByRecord(refundOrder);
        return ApiRes.okWithSign(bizRs, configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()).getAppSecret());
    }
}
